package com.xyy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author xyy
 * @date 2019/4/12
 */
public class Handler implements InvocationHandler{
    private Subject subject;
    public Handler(Subject subject){
        this.subject = subject;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = method.getName();
        if("send".equals(name)){
            beforeSend();
            method.invoke(subject,args);
            afterSend();
        }
        if("receive".equals(name)){
            beforeReceive();
            method.invoke(subject,args);
            afterReceive();
        }
        return null;
    }
    private void beforeSend(){
        System.out.println("before send msg");
    }
    public void afterSend(){
        System.out.println("after send msg");
    }
    private void beforeReceive(){
        System.out.println("before receive msg");
    }
    public void afterReceive(){
        System.out.println("after receive msg");
    }
}
